package blue.mqtt4j.client.codec;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

/**
 * @author Jin Zheng
 * @since 1.0 2020-10-29
 */
public class JsonEncoder implements Encoder
{
	private static SerializerFeature[] serializer = new SerializerFeature[] {
			SerializerFeature.WriteDateUseDateFormat, SerializerFeature.WriteMapNullValue,
			SerializerFeature.WriteClassName};

	public JsonEncoder()
	{
	}

	@Override
	public byte[] encode(Object object)
	{
		if (object == null)
			return new byte[0];

		if (object instanceof byte[])
			return (byte[]) object;

		byte[] bytes = JSON.toJSONBytes(object, serializer);
		return bytes;
	}
}
